import type { ResponseData } from "../typings";
import type { RequestOptionsInit } from "umi-request";
import fetch from "umi-request";
import { message } from "antd";

const request = <T = any>(url: string, options?: RequestOptionsInit): Promise<ResponseData<T>> => {
    return new Promise<ResponseData<T>>((resolve, reject) => {
        let requestUrl = url;
        if (!url.startsWith("http://")) {
            requestUrl = `http://127.0.0.1:5000${url}`;
        }
        fetch<ResponseData<T>>(requestUrl, options).then(res => {
            if (res.success) {
                resolve(res);
            } else {
                console.log('error', res);
                message.error(res.message);
                reject(res);
            }
        });
    });
};

export default request;
